<?php
// TODO: dodawanie, edytowanie, usuwanie userw�w
session_start();
class Controller_Admin_User extends Controller_Admin_Abstract
{	
	public function __construct(Controller_Front $frontController)
	{
		parent::__construct('admin_user', $frontController);
	}
	
	public function indexAction()
	{
		$this->view()->display('admin_useredit.tpl');
	}
	
	public function newAction()
	{
		$useredit;
		$buttonName;
		
		if (isset($_GET['id_user'])) {
			$id = $_GET['id_user'];
			$buttonName = 'Edytuj';
			
			$useredit = UsersPeer::retrieveByPK($id);
		} else {
			$buttonName = 'Dodaj';
			$useredit = new Users();
		}
		
		$this->view()->assign('useredit', $useredit);
		$this->view()->assign('editButton', $buttonName);
		$this->view()->assign('userspermissions', UserspermissionsPeer::doSelect(new Criteria()));
		$_SESSION['enable']=true;
		$this->view()->display('admin_useredit_edit.tpl');
	}
	
	public function listAction()
	{
		$useredit = UsersPeer::doSelect(new Criteria());
		//var_dump($categories);
		$this->view()->assign('useredit', $useredit);
		
		$this->view()->display('admin_useredit_list.tpl');
	}
	
	public function checkAction()
	{		
		$useredit;
		
		if (!empty($_POST['id_user'])) {
			$id = $_POST['id_user'];
			
			$useredit = UsersPeer::retrieveByPK($id);
		} else {
			$useredit = new Users();
		}
		
		if($_SESSION["enable"]==true) {
			$useredit->setLogin($_POST['login']);
			$useredit->setPassword($_POST['password']);
			$useredit->setAvatar($_POST['avatar']);
			$useredit->setEmail($_POST['email']);
			$useredit->setBirthDate($_POST['birthDate']);
			$useredit->setIdUsersPermission($_POST['id_users_permission']);
			$useredit->setIdPreferencesGroup('1');
			$useredit->save();
			$this->ok('Useredit saved.');
			$_SESSION['enable']=false;	
		} else {
			$this->fail('Re-saving it is not possible.');
		}
	}	
	
	public function deleteAction()
	{
		
		if (!isset($_GET['id_user'])) {
			throw new Exception('id not spec');
		}	
		
		$id = $_GET['id_user'];
		try {
			NewsPeer::doDelete($id);
		} catch (Exception $e) {
			$this->view()->assign('fail_info', 'Nie udalo sie usunac ' 
				. $e->getMessage());  	
			return $this->view()->display('fail.tpl');		
		}
					
	}
	
}